<template>
  <div class="app-container">
    <h4 class="form-header h4">基本信息</h4>
    <el-form ref="form" :model="form" label-width="80px">
      <el-row>
        <el-col :span="8" :offset="2">
          <el-form-item label="房屋编号" prop="houseId">
            <el-input v-model="form.houseId" disabled/>
          </el-form-item>
        </el-col>
        <el-col :span="8" :offset="2">
          <el-form-item label="房屋名称" prop="houseName">
            <el-input v-model="form.houseName" disabled/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <h4 class="form-header h4">配套家具信息</h4>
    <el-table v-loading="loading" :row-key="getRowKey" @row-click="clickRow" ref="table"
              :data="supportList.slice((pageNum-1)*pageSize,pageNum*pageSize)"
              @selection-change="handleSelectionChange">
      <el-table-column label="序号" type="index" align="center">
        <template slot-scope="scope">
          <span>{{ (pageNum - 1) * pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column type="selection" :reserve-selection="true" width="55" align="center"/>
      <el-table-column label="家具名称" align="center" prop="supportName">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_house_support" :value="scope.row.supportName"/>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="pageNum" :limit.sync="pageSize"/>

    <el-form label-width="100px">
      <el-form-item style="text-align: center;margin-left:-120px;margin-top:30px;">
        <el-button type="primary" @click="submitForm()">提交</el-button>
        <el-button @click="close()">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {listSupport, updateAllotSupport} from "@/api/system/support";

export default {
  name: "Support",
  dicts: ["sys_house_support"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 分页信息
      pageNum: 1,
      pageSize: 10,
      // 总条数
      total: 0,
      // 房屋家具类表格数据
      supportList: [],
      //选中的家具名称
      supports: [],

      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    //this.getList();
    const houseId = this.$route.params && this.$route.params.houseId;
    if (houseId) {
      this.loading = true;
      listSupport(houseId).then((response) => {
        this.supportList = response.supports;
        this.form = response.house;
        this.total = this.supportList.length;
        this.$nextTick(() => {
          this.supportList.forEach((row) => {
            if (row.flag) {
              this.$refs.table.toggleRowSelection(row);
            }
          });
        });
        this.loading = false;
      });
    }
  },
  methods: {
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.supports = selection.map((item => item.supportName))
    },
    /** 提交按钮 */
    submitForm() {
      const houseId = this.form.houseId;
      const supports = this.supports.join(",");
      updateAllotSupport({houseId: houseId, supports: supports}).then((response) => {
        this.$modal.msgSuccess("修改成功");
        this.close();
      });
    },
    /** 关闭按钮 */
    close() {
      const obj = {path: "/house/houses"};
      this.$tab.closeOpenPage(obj);
    },
    // 保存选中的数据编号
    getRowKey(row) {
      return row.supportName;
    },
    /** 单击选中行数据 */
    clickRow(row) {
      this.$refs.table.toggleRowSelection(row);
    },
  }
};
</script>
